/** @file               sw_serial.h
 *  @copyright          Copyright (C) 2006-2026 flyfish (tenggui_fu@126.com)
 *  @brief              This file is part of the swallow library
 *  @author             $Author: flyfish $
 *  @version            $Rev: 385 $
 *  @date               $Date: 2016-12-22 00:12:05 +0800 (周四, 22 12月 2016) $
 */
#ifndef __SW_SERIAL_H__
#define __SW_SERIAL_H__

#ifdef __cplusplus
extern "C" {
#endif

#ifdef WIN32
typedef HANDLE SW_SERIAL;
#define SW_INVALID_SERIAL (SW_SERIAL)INVALID_HANDLE_VALUE
#else
typedef int SW_SERIAL;
#define SW_INVALID_SERIAL (SW_SERIAL)(-1)
#endif

typedef enum _SW_BAUD_RATE
{
    SW_BR_50,                /**< POSIX ONLY */
    SW_BR_75,                /**< POSIX ONLY */
    SW_BR_110,
    SW_BR_134,               /**< POSIX ONLY */
    SW_BR_150,               /**< POSIX ONLY */
    SW_BR_200,               /**< POSIX ONLY */
    SW_BR_300,
    SW_BR_600,
    SW_BR_1200,
    SW_BR_1800,              /**< POSIX ONLY */
    SW_BR_2400,
    SW_BR_4800,
    SW_BR_9600,
    SW_BR_14400,             /**< WINDOWS ONLY */
    SW_BR_19200,
    SW_BR_38400,
    SW_BR_56000,             /**< WINDOWS ONLY */
    SW_BR_57600,
    SW_BR_76800,             /**< POSIX ONLY */
    SW_BR_115200,
    SW_BR_128000,            /**< WINDOWS ONLY */
    SW_BR_256000,            /**< WINDOWS ONLY */
    SW_BR_INVALID = -1
}SW_BAUD_RATE;

typedef enum _SW_DATA_BITS
{
    SW_DB_5,
    SW_DB_6,
    SW_DB_7,
    SW_DB_8,
    SW_DB_INVALID = -1
}SW_DATA_BITS;

typedef enum _SW_STOP_BITS
{
    SW_SB_1,
    SW_SB_1_5,
    SW_SB_2,
    SW_SB_INVALID = -1
}SW_STOP_BITS;

typedef enum _SW_PARITY
{
    SW_P_NONE,
    SW_P_ODD,
    SW_P_EVEN,
    SW_P_MARK,               /**< WINDOWS ONLY */
    SW_P_SPACE,
    SW_P_INVALID = -1
}SW_PARITY;

typedef enum _SW_PURGE_FLAG
{
    SW_PF_RXABORT = 0x2,
    SW_PF_RXCLEAR = 0x8,
    SW_PF_TXABORT = 0x1,
    SW_PF_TXCLEAR = 0x4
}SW_PURGE_FLAG;

SW_API SW_RET SW_InitSerialAPI(SW_VOID);
SW_API SW_RET SW_RunSerialAPI(SW_VOID);
SW_API SW_RET SW_StopSerialAPI(SW_VOID);
SW_API SW_RET SW_ExitSerialAPI(SW_VOID);
SW_API SW_SERIAL SW_OpenSerial(SW_UINT32 port, SW_BAUD_RATE baudRate, SW_DATA_BITS dataBits
                               , SW_STOP_BITS stopBits, SW_PARITY parity, SW_BOOL hardFlowCtrl
                               , SW_BOOL softFlowCtrl, SW_BOOL nonBlock, const SW_CHAR *pPortName);
SW_API SW_RET SW_CloseSerial(SW_SERIAL sd);
SW_API SW_RET SW_SetSerialBaudRate(SW_SERIAL sd, SW_BAUD_RATE baudRate);
SW_API SW_BAUD_RATE SW_GetSerialBaudRate(SW_SERIAL sd);
SW_API SW_RET SW_SetSerialDataBits(SW_SERIAL sd, SW_DATA_BITS dataBits);
SW_API SW_DATA_BITS SW_GetSerialDataBits(SW_SERIAL sd);
SW_API SW_RET SW_SetSerialStopBits(SW_SERIAL sd, SW_STOP_BITS stopBits);
SW_API SW_STOP_BITS SW_GetSerialStopBits(SW_SERIAL sd);
SW_API SW_RET SW_SetSerialParity(SW_SERIAL sd, SW_PARITY parity);
SW_API SW_PARITY SW_GetSerialParity(SW_SERIAL sd);
SW_API SW_RET SW_EnableSerialHardFlowCtrl(SW_SERIAL sd, SW_BOOL enable);
SW_API SW_BOOL SW_IsSerialHardFlowCtrlEnabled(SW_SERIAL sd);
SW_API SW_RET SW_EnableSerialSoftFlowCtrl(SW_SERIAL sd, SW_BOOL enable);
SW_API SW_BOOL SW_IsSerialSoftFlowCtrlEnabled(SW_SERIAL sd);
SW_API SW_UINT32 SW_GetSerialBytesAvailable(SW_SERIAL sd);
SW_API SW_RET SW_EnableSerialNonBlock(SW_SERIAL sd, SW_BOOL enable);
SW_API SW_BOOL SW_IsSerialNonBlockEnabled(SW_SERIAL sd);
SW_API SW_RET SW_EnableSerialRtsHighWrite(SW_SERIAL sd, SW_BOOL enable);
SW_API SW_BOOL SW_IsSerialRtsHighWriteEnabled(SW_SERIAL sd);
SW_API SW_UINT32 SW_ReadSerial(SW_SERIAL sd, SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_WriteSerial(SW_SERIAL sd, const SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_ReadSerialN(SW_SERIAL sd, SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_WriteSerialN(SW_SERIAL sd, const SW_VOID *pBuf, SW_UINT32 bufLen);
SW_API SW_UINT32 SW_TimeReadSerial(SW_SERIAL sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeWriteSerial(SW_SERIAL sd, const SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeReadSerialN(SW_SERIAL sd, SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_UINT32 SW_TimeWriteSerialN(SW_SERIAL sd, const SW_VOID *pBuf, SW_UINT32 bufLen, SW_UINT32 msec);
SW_API SW_RET SW_SetSerialRtsPinLevel(SW_SERIAL sd, SW_PIN_LEVEL pinLevel);
SW_API SW_PIN_LEVEL SW_GetSerialRtsPinLevel(SW_SERIAL sd);
SW_API SW_RET SW_PurgeSerial(SW_SERIAL sd, SW_PURGE_FLAG flags);
SW_API const SW_CHAR *SW_BaudRateToStr(SW_BAUD_RATE baudRate);
SW_API SW_BAUD_RATE SW_StrToBaudRate(const SW_CHAR *pStr);
SW_API const SW_CHAR *SW_DataBitsToStr(SW_DATA_BITS dataBits);
SW_API SW_DATA_BITS SW_StrToDataBits(const SW_CHAR *pStr);
SW_API const SW_CHAR *SW_StopBitsToStr(SW_STOP_BITS stopBits);
SW_API SW_STOP_BITS SW_StrToStopBits(const SW_CHAR *pStr);
SW_API const SW_CHAR *SW_ParityToStr(SW_PARITY parity);
SW_API SW_PARITY SW_StrToParity(const SW_CHAR *pStr);

#ifdef __cplusplus
}
#endif

#endif

